;iN2Fun's Crafter V.02
;Please let me know of any problems or ideas you have to improve.
;
;=For Crafting Gem tab=
;Must have in front of gem crafter, with gem combiner open.
;Change show to ALL so jsut see gems can craft
;
;=For Item Crafter tab=
;Must have craft window open with item selected.
;

;Includes
#RequireAdmin
#include <ImageSearch.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$Form1 = GUICreate("[iN2Fun] Crafter", 273, 350)
GUISetBkColor(0xFFFFFF)

$hTab = GUICtrlCreateTab(8, 10, 260, 335)
;Create Item Crafter tab
GUICtrlCreateTabItem("Item Crafter")
$howmany = GUICtrlCreateInput("0", 85, 100, 100, 40)
GUICtrlSetTip($howmany, "Amount to craft. Leave on 0 for unlimited.", "", 1)
GUICtrlSetFont(-1, 20, 800, 0, "Arial")
$itemcraft = GUICtrlCreateButton("Craft!", 56, 200, 161, 49, $WS_GROUP)
GUICtrlSetTip($itemcraft, "Push Craft button when ready to craft. F8 to stop.", "", 1)
GUICtrlSetFont(-1, 24, 800, 0, "Arial")
; Close Tab definiton
GUICtrlCreateTabItem("")
GUISetState()
; Create Gem Crafter tab
GUICtrlCreateTabItem("Gem Crafter")
$Amethyst = GUICtrlCreateCheckbox("Amethyst", 56, 56, 161, 49)
GUICtrlSetFont(-1, 24, 800, 0, "Times New Roman")
GUICtrlSetTip($Amethyst, "Craft all Amethyst gems", "", 1)
$Emerald = GUICtrlCreateCheckbox("Emerald", 56, 104, 137, 49)
GUICtrlSetFont(-1, 24, 800, 0, "Times New Roman")
GUICtrlSetTip($Emerald, "Craft all Emerald gems", "", 1)
$Ruby = GUICtrlCreateCheckbox("Ruby", 56, 152, 113, 49)
GUICtrlSetFont(-1, 24, 800, 0, "Times New Roman")
GUICtrlSetTip($Ruby, "Craft all Ruby gems", "", 1)
$Topaz = GUICtrlCreateCheckbox("Topaz", 56, 200, 185, 49)
GUICtrlSetFont(-1, 24, 800, 0, "Times New Roman")
GUICtrlSetTip($Topaz, "Craft all Topaz gems", "", 1)
$ButtonCraft = GUICtrlCreateButton("Craft!", 56, 280, 161, 49, $WS_GROUP)
GUICtrlSetTip($ButtonCraft, "Push Craft button when ready to craft. F8 to stop.", "", 1)
GUICtrlSetFont(-1, 24, 800, 0, "Arial")
; Close Tab definiton
GUICtrlCreateTabItem("")
GUISetState()

;Variables
Global $delay = 3600 ;Delay to wait after click craft
Global $x1 = 0
Global $y1 = 0
Global $Run = 0

;Hotkeys
HotKeySet("{F8}", "StopScript") ; Stop script

While 1
	$Msg = GUIGetMsg()
	If $Msg = -3 Then Exit
	If $Msg = $ButtonCraft Then buttoncraft1() ;Gem craft button
	If $Msg = $itemcraft Then itemcraft1() ; Item craft button
	$craftamount = GUICtrlRead($howmany) ;How many to craft
WEnd

Func StopScript() ;A way to stop the script if need
	$Run = 0
EndFunc   ;==>StopScript

;=== Gem Craft Tab code
Func buttoncraft1() ;Craft button pushed
	$Run = 1
	buttoncraft2()
EndFunc   ;==>buttoncraft1

Func buttoncraft2() ;Spamming craft button till done, then selects next gem
	While $Run = 1
		ToolTip("Crafting|Press F8 to Stop", 200, 200, "", 1, 2)
		If _ImageSearch("Images/CrButton.png", 1, $x1, $y1, 100) Then
			MouseClick("Left", $x1, $y1, 1, 5)
		Else
			amethselect()
		EndIf
		Sleep($delay)
		ToolTip("")
	WEnd
EndFunc   ;==>buttoncraft2

Func amethselect() ;Searchs for Amethyst gems to craft
	If GUICtrlRead($Amethyst) = $GUI_CHECKED Then
		While $Run = 1
			ToolTip("Searching for Amethyst|Press F8 to Stop", 200, 200, "", 1, 2)
			If _ImageSearch("Images/1ameth.png", 1, $x1, $y1, 100) Then
				MouseClick("Left", $x1, $y1, 1, 5)
				Sleep(500)
				buttoncraft2()
			Else
				emerselect()
			EndIf
		WEnd
	Else
		emerselect()
	EndIf
EndFunc   ;==>amethselect

Func emerselect() ;Searchs for Emerald gems to craft
	If GUICtrlRead($Emerald) = $GUI_CHECKED Then
		While $Run = 1
			ToolTip("Searching for Emerald|Press F8 to Stop", 200, 200, "", 1, 2)
			If _ImageSearch("Images/1emer.png", 1, $x1, $y1, 100) Then
				MouseClick("Left", $x1, $y1, 1, 5)
				Sleep(500)
				buttoncraft2()
			Else
				rubyselect()
			EndIf
		WEnd
	Else
		rubyselect()
	EndIf
EndFunc   ;==>emerselect

Func rubyselect() ;Searchs for Ruby gems to craft
	If GUICtrlRead($Ruby) = $GUI_CHECKED Then
		While $Run = 1
			ToolTip("Searching for Ruby|Press F8 to Stop", 200, 200, "", 1, 2)
			If _ImageSearch("Images/1ruby.png", 1, $x1, $y1, 100) Then
				MouseClick("Left", $x1, $y1, 1, 5)
				Sleep(500)
				buttoncraft2()
			Else
				topazselect()
			EndIf
		WEnd
	Else
		topazselect()
	EndIf
EndFunc   ;==>rubyselect

Func topazselect() ;Searchs for Topaz gems to craft
	If GUICtrlRead($Topaz) = $GUI_CHECKED Then
		While $Run = 1
			ToolTip("Searching for Topaz|Press F8 to Stop", 200, 200, "", 1, 2)
			If _ImageSearch("Images/1topaz.png", 1, $x1, $y1, 100) Then
				MouseClick("Left", $x1, $y1, 1, 5)
				Sleep(500)
				buttoncraft2()
			Else
				ToolTip("")
				MsgBox(0, "", "Done!")
				$Run = 0
			EndIf
		WEnd
	Else
		ToolTip("")
		MsgBox(0, "", "Done!")
		$Run = 0
	EndIf
EndFunc   ;==>topazselect
;== End Gem Craft tab Code
;==
;== Item Craft Tab code
Func itemcraft1() ;Craft button pushed
	$Run = 1
	itemcraft2()
EndFunc   ;==>itemcraft1

Func itemcraft2() ;Spamming craft button till done
	While $Run = 1
		Do
			ToolTip($craftamount & " Remaining to craft|Press F8 to Stop", 200, 200, "", 1, 2)
			If _ImageSearch("Images/CrButton.png", 1, $x1, $y1, 100) Then
				MouseClick("Left", $x1, $y1, 1, 5)
				$craftamount = $craftamount - 1
			Else
				MsgBox(0, "", "Out of mats?")
				$Run = 0
			EndIf
			Sleep($delay)
			ToolTip("")
		Until $craftamount = 0
		MsgBox(0, "", "Crafting complete!")
		$Run = 0
	WEnd
EndFunc   ;==>itemcraft2